package com.yx.backstage.converter;

import cn.hutool.core.date.DateUtil;
import com.yx.backstage.model.UserMessage;
import com.yx.backstage.model.param.UserMessageParam;
import com.yx.backstage.model.vo.UserMessageVo;
import com.yx.backstage.enums.Sex;
import com.yx.backstage.util.TimeFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author YangXiong
 */
public class UserMessageConverter {
    public static List<UserMessageVo> handlerPageResult(List<UserMessage> records) {
        List<UserMessageVo> result = new ArrayList<>();
        records.forEach(record -> {
            UserMessageVo userMessageVo = userToVo(record);
            result.add(userMessageVo);
        });
        return result;
    }

    public static UserMessageVo userToVo(UserMessage record) {
        UserMessageVo messageVo = new UserMessageVo();
        messageVo.setId(record.getId());
        messageVo.setUserId(record.getUserId());
        messageVo.setUserName(record.getUserName());
        messageVo.setEnable(record.getEnable());
        String format = DateUtil.format(record.getBirthday(), TimeFormat.DATE_FORMAT);
        messageVo.setBirthday(format);
        messageVo.setIntroduction(record.getIntroduction());
        messageVo.setEmail(record.getEmail());
        messageVo.setHobby(record.getHobby());
        messageVo.setInterview(record.getInterview());
        messageVo.setPhone(record.getPhone());
        messageVo.setHeadImgUrl(record.getHeadImgUrl());
        messageVo.setOperatorName(record.getOperatorName());
        messageVo.setSex(Sex.getValueByKey(record.getSex()));
        messageVo.setOperatorId(record.getOperatorId());
        messageVo.setEnable(record.getEnable());
        Date modifyTime = record.getModifyTime();
        messageVo.setModifyTime(DateUtil.format(modifyTime, TimeFormat.TIME_FORMAT));
        Date createTime = record.getCreateTime();
        messageVo.setCreateTime(DateUtil.format(createTime, TimeFormat.TIME_FORMAT));
        return messageVo;
    }

    public static void handlerRequest(UserMessage user, UserMessageParam param) {
        user.setId(param.getId());
        user.setUserId(param.getUserId());
        user.setUserName(param.getUserName());
        user.setEnable(param.getEnable());
        user.setBirthday(DateUtil.parse(param.getBirthday()));
        user.setEmail(param.getEmail());
        user.setIntroduction(param.getIntroduction());
        user.setHobby(param.getHobby());
        user.setInterview(param.getInterview());
        user.setPhone(param.getPhone());
        user.setHeadImgUrl(param.getHeadImgUrl());
        user.setOperatorName(param.getOperatorName());
        user.setSex(Sex.getKeyByValue(param.getSex()));
        user.setOperatorId(param.getOperatorId());
        user.setEnable(param.getEnable());
    }
}
